﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Resources;
using System.Globalization;

namespace LPV_POS.mh_pos.mh_cauhinh
{
    public partial class mh_dm_mayin_bep_info : DevExpress.XtraEditors.XtraForm
    {
        CultureInfo culture;
        DataTable tbMain = new DataTable();
        string ma = "";//PK_: giá trị này dc truyền từ bảng danh sách, trường hợp EDIT
        bang obang;

        #region "Muti language"

        private void SetLanguage_banhang(string cultureName)
        {
            try
            {
                culture = CultureInfo.CreateSpecificCulture(cultureName);
                ResourceManager rm = new ResourceManager("LPV_POS.Muti_language.MyResource", typeof(mh_dm_mayin_bep_info).Assembly);
                lblOBJNAME.Text = rm.GetString("ten", culture);
                lblCOMNAME.Text = rm.GetString("cong", culture);
                label1.Text = rm.GetString("tenmayin", culture);
                lblPRINTCOUNT.Text = rm.GetString("sobanin", culture);

                chkIn_TachGop.Text = rm.GetString("inkhitachgopban", culture);
                lbel_tencongty.Text = rm.GetString("thongtinmayin", culture);
                bton_luu.Text = rm.GetString("luu", culture);
                btnThoat.Text = rm.GetString("thoat", culture);
               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Note");
            }
        }

        #endregion

        public mh_dm_mayin_bep_info(bang _obang, string _ma)
        {
            InitializeComponent();
            culture = CultureInfo.CurrentCulture;
            ma = _ma;
            obang = _obang;
        }
        public enum bang
        {
            DM_MAYIN_HoaDon,
            DM_MAYIN_Bep,
            DM_MAYIN_BienNhan
        }
        private void mh_dm_mayin_bep_info_Load(object sender, EventArgs e)
        {
            cpublic ocpublic = new cpublic();
            string str_lang = ocpublic.Registry_Get("mh_main_lang");
            if (str_lang == "")
                str_lang = "vi-VN";
            SetLanguage_banhang(str_lang);
            this.showinfo();
        }

        private void bton_luu_Click(object sender, EventArgs e)
        {
            if (this.save() > 0)
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }
        #region"    showinfo"
        void showinfo()
        {
            this.showdata();
            if (tbMain.Rows.Count == 0)
            {
                //txtMAMAY.Text = "";
                //txtOBJNAME.Text = "";
                //txtCOMNAME.Text = "";
                //cboBAUDRATE.Text = 0;
                //cboDATABITS.Text = 0;
                //cboPARITYBITS.Text = 0;
                //cboSTOPBITS.Text = 0;
                //cboFLOWCONTROL.Text = 0;
                //numTIMEOUT.Text = 0;
                //cboPRINTCOUNT.Text = 0;
                //txtREMARK.Text = "";
                return;
            }
            cboPOS.Text = tbMain.Rows[0]["POS_NUMBER"] + "";
            txtOBJNAME.Text = tbMain.Rows[0]["OBJNAME"] + "";
            txtCOMNAME.Text = tbMain.Rows[0]["COMNAME"] + "";
            if (tbMain.Columns.Contains("PRINTER_NAME"))
                cboPrinter_Name.Text = tbMain.Rows[0]["PRINTER_NAME"] + "";

            //cboBAUDRATE.Text = decimal.Parse(string.Concat("0" + ).Replace("0-", "-"));
            cboBAUDRATE.Text=tbMain.Rows[0]["BAUDRATE"]+"";
            cboDATABITS.Text = tbMain.Rows[0]["DATABITS"]+"";
            cboPARITYBITS.SelectedIndex = int.Parse(string.Concat("0" + tbMain.Rows[0]["PARITYBITS"]).Replace("0-", "-"));
            cboSTOPBITS.Text = int.Parse("0" + tbMain.Rows[0]["STOPBITS"].ToString().Replace(".0000", "")) + "";
            string sFLOWCONTROL = tbMain.Rows[0]["FLOWCONTROL"] + "";
            cboFLOWCONTROL.SelectedIndex = int.Parse(sFLOWCONTROL.Replace(".0000", ""));
            numTIMEOUT.Value = decimal.Parse(tbMain.Rows[0]["TIMEOUT"].ToString().Replace("0-", "-").Replace(".0000", ""));
            cboPRINTCOUNT.SelectedIndex = int.Parse("0" + tbMain.Rows[0]["PRINTOUT"].ToString().Replace(".0000", ""));
            

            Boolean bIN_KHI_TACHGOP_BAN = false;//20101108
            Boolean bIN_KHI_KET_NOI_TRUC_TIEP_DEN_MAY_NAY = false;
            Boolean.TryParse(tbMain.Rows[0]["IN_KHI_TACHGOP_BAN"] + "", out bIN_KHI_TACHGOP_BAN);
            Boolean.TryParse(tbMain.Rows[0]["IN_KHI_KET_NOI_TRUC_TIEP_DEN_MAY_NAY"] + "", out bIN_KHI_KET_NOI_TRUC_TIEP_DEN_MAY_NAY);
            chkIn_TachGop.Checked = bIN_KHI_TACHGOP_BAN;
            chkIn_KetNoiTrucTiep.Checked = bIN_KHI_KET_NOI_TRUC_TIEP_DEN_MAY_NAY;
        }
        #endregion

        #region"    showdata"
        void showdata()
        {
            string sp_Name = "";
            string key_Name = "";

            if (obang == bang.DM_MAYIN_Bep)
            {
                sp_Name = "sp_DM_MAYIN_BEP";
                key_Name = "DM_MAYIN_BEP_MA";
            }
            else if (obang == bang.DM_MAYIN_HoaDon)
            {
                sp_Name = "sp_DM_MAYIN_HD";
                key_Name = "DM_MAYIN_HD_MA";
            }
            else if (obang == bang.DM_MAYIN_BienNhan)
            {
                sp_Name = "sp_DM_MAYIN_BIENNHAN";
                key_Name = "DM_MAYIN_BIENNHAN_MA";
            }

            tbMain = new thuvien().SP_SelectDataTable(sp_Name,
                                                           new string[] { "@flag", "@" + key_Name },
                                                           new object[] { 4, ma });
        }
        #endregion

        #region"    save"
        int save()
        {
            int _iReasult = -1;
            string sp_Name = "";
            string key_Name = "";

            if (obang == bang.DM_MAYIN_Bep)
            {
                sp_Name = "sp_DM_MAYIN_BEP";
                key_Name = "DM_MAYIN_BEP_MA";
            }
            else if (obang == bang.DM_MAYIN_HoaDon)
            {
                sp_Name = "sp_DM_MAYIN_HD";
                key_Name = "DM_MAYIN_HD_MA";
            }
            else if (obang == bang.DM_MAYIN_BienNhan)
            {
                sp_Name = "sp_DM_MAYIN_BIENNHAN";
                key_Name = "DM_MAYIN_BIENNHAN_MA";
            }
            _iReasult = new thuvien().SP_Update(sp_Name,
                                     new string[] { "@flag", "@PARITYBITS", "@"+key_Name, 
                                                        "@MAMAY", "@OBJNAME", "@COMNAME", 
                                                        "@BAUDRATE", "@DATABITS", "@STOPBITS", 
                                                        "@FLOWCONTROL", "@TIMEOUT", "@PRINTOUT",
                                                        "@POS_NUMBER","@IN_KHI_TACHGOP_BAN", "@IN_KHI_KET_NOI_TRUC_TIEP_DEN_MAY_NAY",
                                                        "@PRINTER_NAME"
                                     },
                                     new object[] { 1, cboPARITYBITS.SelectedIndex, ma,
                                                        Variable.strComputer_Code, txtOBJNAME.Text, txtCOMNAME.Text,
                                                        cboBAUDRATE.Text, cboDATABITS.Text, cboSTOPBITS.Text, 
                                                        cboFLOWCONTROL.SelectedIndex, numTIMEOUT.Value, cboPRINTCOUNT.SelectedIndex,
                                                        cboPOS.Text,chkIn_TachGop.Checked,chkIn_KetNoiTrucTiep.Checked,
                                                        cboPrinter_Name.Text
                                     });
            ma = _iReasult + "";
            //cboPrinter_Name.Text = tbMain.Rows[0]["PRINTER_NAME"] + "";



            if (_iReasult == -1)
                return _iReasult;

            return 1;
        }
        #endregion

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void bton_dautrang_Click(object sender, EventArgs e)
        {
            mh_chu omh_chu = new mh_chu();
            string s = omh_chu.ShowForm(txtOBJNAME.Text, 200, 200, false);
            if (omh_chu.DialogResult == DialogResult.OK)
                txtOBJNAME.Text = s;
        }
    }
}